import 'package:fate_blind_box/network/http_request.dart';
import 'package:fate_blind_box/routes/page_route_names.dart';
import 'package:fate_blind_box/theme/theme_color.dart';
import 'package:fate_blind_box/utils/common_widget.dart';
import 'package:fate_blind_box/utils/loading.dart';
import 'package:fate_blind_box/utils/provider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../network/Api.dart';
import '../groupchat.dart';

class AddNewGroupPage extends StatefulWidget {
  String groupId = '';

  AddNewGroupPage({this.groupId = '', Key? key}) : super(key: key);

  @override
  State<AddNewGroupPage> createState() => _AddNewGroupPageState();
}

class _AddNewGroupPageState extends State<AddNewGroupPage> {
  Map groupResMap = {};

  @override
  Widget build(BuildContext context) {
    return AppWidget.navTitleBar(
      childWidget: FutureBuilder(
          future: getData(),
          builder: (BuildContext context, AsyncSnapshot<Map> snapshot) {
            Map resMap = {};
            if (snapshot.data != null) {
              resMap = snapshot.data as Map;
            }
            return snapshot.hasData
                ? Column(
                    children: [
                      AppWidget.commonEmptyImg(
                          url: resMap['groupUrl'], imgWidth: 62, imgHeight: 62),
                      const SizedBox(
                        height: 13,
                      ),
                      Text(
                        resMap['processName'],
                        style: const TextStyle(
                            fontSize: 18, color: Color(0xFF1B1B1B)),
                      ),
                      Expanded(child: Container()),
                      GestureDetector(
                        onTap: () {
                          addGroupAction();
                        },
                        child: Container(
                          width: 176,
                          height: 45,
                          decoration: BoxDecoration(
                              color: ThemeColor.appColor,
                              borderRadius: BorderRadius.circular(5)),
                          alignment: Alignment.center,
                          child: const Text(
                            '加入群聊',
                            style: TextStyle(color: Colors.white, fontSize: 16),
                          ),
                        ),
                      ),
                      SizedBox(
                        height: ScreenUtil().statusBarHeight + 42,
                      ),
                    ],
                  )
                : Container();
          }),
    );
  }

  @override
  void initState() {
    super.initState();
  }

//获取群信息
  Future<Map> getData() async {
    Map resMap = {};
    var res = await HttpRequest.request('easemob/qrcode/addGroupProcess',
        params: {"groupId": widget.groupId});
    if (res['data'] != null) {
      resMap = res['data'];
    }
    groupResMap = resMap;
    return resMap;
  }

  //扫码加群
  void addGroupAction() async {
    Map<String, dynamic> dataMap = {
      'groupId': widget.groupId,
      'username': Provider.userEaseName,
      'e': {
        'type': 1 / 2,
        'id': '',
      }
    };
    var res = await HttpRequest.request('easemob/qrcode/addGroupMember',
        params: dataMap);
    if (res['code'] != null && res['code'] == 0) {
      //获取好友列表成功后调用群列表后台加缓存
      await Api.getInstance().getGroupList(Provider.userEaseName);
      Get.to(GroupChat(widget.groupId.toString(), groupResMap['owner']),
          routeName: PageRouteNames.groupChatPage);
    } else {
      final msg = res['msg'] ?? '您已入群，无需加入';
      toast(msg);
    }
  }
}
